﻿namespace ViaConvert.Test.Web.Controllers
{
    using Models;
    using System.IO;
    using System.Web.Caching;
    using System.Web.Mvc;
    
    /// <summary>
    /// Mvc Based
    /// </summary>
    public class ConvertController : Controller
    {        
        #region Default
        private Message message;        
        public ConvertController()
        {
            message = new Message();
        }
        #endregion

        #region Action
        [HttpPost]
        public JsonResult Post(Form form)
        {
            if (ModelState.IsValid)
            {
                if (fileIsValid(form))
                {
                    ContentFile file = readFile(form);

                    #region Web Api Usage
                    
                    object export = "";
                    
                    if (form.toFormat == "csv")
                    {
                        #region ViaConvert To Csv Content
                        export = ViaConvert
                        .Init
                        .Content(file.content)
                        .ToCsvContent();
                        #endregion
                    }

                    if (form.toFormat == "xml")
                    {
                        #region ViaConvert To Xml Content
                        export = ViaConvert
                        .Init
                        .Content(file.content)
                        .ToXmlContent();
                        #endregion
                    }

                    if (form.toFormat == "json")
                    {
                        #region ViaConvert To Json Content
                        export = ViaConvert
                        .Init
                        .Content(file.content)
                        .ToJsonContent();
                        #endregion
                    }                    
                    #endregion
                    
                    returnMessage("File Accepted", file.contentLength, file.contentType, file.fileName, file.content, export);

                }
                else
                {
                    returnMessage("File Media Type Error", "Fail", Server.GetLastError());
                }
            }
            else
            {
                returnMessage("Form value is empty", "Fail", "Fail");
            }

            return Json(message, JsonRequestBehavior.AllowGet);
        }        
        #endregion

        #region Method
        private bool fileIsValid(Form model)
        {
            bool ret = false;

            try
            {
                if (model.sourceFile != null)
                {
                    if (model.sourceFile[0] != null)
                    {
                        ret = true;
                    }
                    else
                    {
                        ret = false;
                    }
                }
                else
                {
                    ret = false;
                }
            }
            catch
            {
                ret = false;
            }
            
            return ret;
        }

        /// <summary>
        /// Read Posted File
        /// </summary>
        /// <param name="model">form.model</param>
        /// <returns>string content</returns>
        private ContentFile readFile(Form model)
        {
            ContentFile result = new ContentFile();

            result.content = new StreamReader(model.sourceFile[0].InputStream).ReadToEnd();
            result.contentLength = model.sourceFile[0].ContentLength;
            result.contentType = model.sourceFile[0].ContentType;
            result.fileName = model.sourceFile[0].FileName;
            
            return result;
        }
        #endregion

        #region Shared Method
        private Message returnMessage(string statusText,int contentLength, string contentType, string fileName, string content, object export)
        {
            message.statusText = statusText;
            message.file.contentLength = contentLength;
            message.file.contentType= contentType;
            message.file.fileName= fileName;

            message.file.content = content;
            message.export = export;

            return message;
        }

        private Message returnMessage(string statusText, string responseText, object responseObject)
        {
            message.responseObject = responseObject;
            message.responseText = responseText;
            message.statusText = statusText;

            return message;
        }

        private Message returnMessage(string statusText, string responseText, object responseObject, int readyState, int Status)
        {
            message.readyState = readyState;
            message.responseObject = responseObject;
            message.responseText = responseText;
            message.Status = Status;
            message.statusText = statusText;

            return message;
        }

        #endregion
    }
}
